package com.dsh.word.Test;

import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import static com.deepoove.poi.util.TableTools.mergeCellsHorizonal;
import static com.deepoove.poi.util.TableTools.mergeCellsVertically;

public class WordTableMergeExample {

    public static void main(String[] args) {
        try (XWPFDocument document = new XWPFDocument()) {
            //创建 5*5的表格
            XWPFTable table = document.createTable(5, 5);
            CTTblPr tblPr = table.getCTTbl().addNewTblPr();
            CTTblWidth Tblwidth = tblPr.addNewTblW();
            Tblwidth.setW(BigInteger.valueOf(8000));
            Tblwidth.setType(STTblWidth.DXA);
            //设置第一行
            XWPFTableRow headerRow = table.getRow(0);
            headerRow.getCell(0).setText("合并单元格实例");
            // 合并首行标题栏
//            mergeCellHorizontal(table, 0, 0, 5);
            mergeCellsHorizonal(table ,0,0,4);
            //设置第二行
            String[] headers = {"姓名", "年龄", "性别", "职业", "联系方式"};
            XWPFTableRow colHeaderRow = table.getRow(1);
            for (int i = 0; i < headers.length; i++) {
                colHeaderRow.getCell(i).setText(headers[i]);
                //样式
            }
            // 填充数据行
            String[][] data = {
                    {"张三", "30", "男", "工程师", "13800138000"},
                    {"李四", "25", "女", "设计师", "13900139000"},
                    {"王五", "40", "男", "经理", "13700137000"},
                    {"赵六", "28", "女", "会计", "13600136000"}
            };

            for (int i = 1; i < data.length; i++) {
                XWPFTableRow dataRow = table.getRow(i);
                for (int j = 0; j < data[i].length; j++) {
                    dataRow.getCell(j).setText(data[i][j]);
                 }
            }
            //合并第一列的3-5纵向合并
            //  写好的方法
            mergeCellsVertically(table, 0, 2, 4);
            try (FileOutputStream out = new FileOutputStream("word_table_merge_example.docx")) {
                document.write(out);
                System.out.println("文档生成成功！");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * @param table    表格
     * @param row      索引行
     * @param startCol 开始
     * @param engCol   结束
     */
//    private static void mergeCellHorizontal(XWPFTable table, int row, int startCol, int engCol) {
//
//        for (int i = startCol; i < engCol; i++) {
//            XWPFTableCell cell = table.getRow(row).getCell(i);
//            if (i == startCol) {
//                //启示单元格
//                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//            }else {
//                //合并后的单元格
//                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
//            }
//
//        }
//
//    }


}
